package com.springfreemwork.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author gpo12345678@163.com
 * @create 2024-03-20 16:46
 */
public class JDKProxy implements InvocationHandler {

	private final Object targe;

	public JDKProxy(Object targe) {
		this.targe = targe;

	}


	public static  <T> T getInstance(T target){
		return (T)Proxy.newProxyInstance(JDKProxy.class.getClassLoader(),target.getClass().getInterfaces(),new JDKProxy(target));
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		before();
		Object invoke = method.invoke(targe, args);
		after();
		return invoke;
	}


	void before(){
		System.out.println("前置");
	}

	void after(){
		System.out.println("后置");

	}
}
